import { FileItemShare } from '@/ts/base/model';
import { ISysFileInfo } from '../systemfile';

export abstract class FileFilter<T extends string> {
  readonly fileTypes: readonly string[];
  readonly groupName: T;
  constructor(groupName: T, fileTypes: readonly string[]) {
    this.groupName = groupName;
    this.fileTypes = fileTypes;
  }

  abstract isType(files: FileItemShare): boolean;

  filter(files: ISysFileInfo[]): ISysFileInfo[] {
    return files.filter((file) => this.isType(file.filedata));
  }
}

class OfficeFiles extends FileFilter<'office'> {
  constructor() {
    super('office', [
      '.doc', '.docx',
      '.xls', '.xlsx',
      '.ppt', '.pptx',
      '.pdf',
      '.md',
    ]);
  }
  isType(file: FileItemShare) {
    return this.fileTypes.includes(
      (file.extension || '-').toLowerCase(),
    );
  }
}
class ImageFiles extends FileFilter<'image'> {
  constructor() {
    super('image', ['image/']);
  }

  isType(file: FileItemShare) {
    return file.contentType?.startsWith('image/') ?? false;
  }
}
class VideoFiles extends FileFilter<'video'> {
  constructor() {
    super('video', ['video/']);
  }

  isType(file: FileItemShare) {
    return file.contentType?.startsWith('video/') ?? false;
  }
}
class AudioFiles extends FileFilter<'audio'> {
  constructor() {
    super('audio', ['audio/']);
  }

  isType(file: FileItemShare) {
    return file.contentType?.startsWith('audio/') ?? false;
  }
}


export const filters = [
  new OfficeFiles(), 
  new ImageFiles(), 
  new VideoFiles(),
  new AudioFiles(),
] as const;

export function getFileType(file: FileItemShare) {
  for (const filter of filters) {
    if (filter.isType(file)) {
      return filter.groupName;
    }
  }
}

export type GroupNames = (typeof filters)[number]['groupName'];

export const groupList: {
  name: GroupNames | '';
  label: string;
}[] = [
  {
    name: '',
    label: '全部',
  },
  {
    name: 'image',
    label: '图片',
  },
  {
    name: 'office',
    label: '文档',
  },
  {
    name: 'video',
    label: '视频',
  },
  {
    name: 'audio',
    label: '音频',
  },
  // {
  //   name: 'other',
  //   label: '其他',
  // },
];
